package com.zjsru.oneDay;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 *  两句话中的不常见单词
 *  如果某个单词在其中一个句子中恰好出现一次，在另一个句子中却 没有出现 ，那么这个单词就是 不常见的 。
 *
 *  输入：s1 = "this apple is sweet", s2 = "this apple is sour"
 * 输出：["sweet","sour"]
 *
 * 输入：s1 = "apple apple", s2 = "banana"
 * 输出：["banana"]
 */
public class uncommonFromSentences {
    public String[] uncommonFromSentences(String s1, String s2) {
        Map<String,Integer> freq = new HashMap<String,Integer>();
        insert(s1,freq);
        insert(s2,freq);

        List<String> ans = new ArrayList<String>();
        for(Map.Entry<String,Integer> entry : freq.entrySet()){
            if(entry.getValue() == 1){
                ans.add(entry.getKey());
            }
        }
        return ans.toArray(new String[0]);
    }

    private void insert(String s, Map<String, Integer> freq) {
        String[] arr = s.split(" ");
        for (String word: arr) {
            freq.put(word,freq.getOrDefault(word,0) + 1);
        }
    }

    public static void main(String[] args){
        uncommonFromSentences uncommonFromSentences = new uncommonFromSentences();
        String s1 = "this apple is sweet";
        String s2 = "this apple is sour";
        System.out.println(uncommonFromSentences.uncommonFromSentences(s1,s2));
    }
}
